package com.yzs.core.ext

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ColorUtils
import com.yzs.core.momory.AppManager
import kotlinx.coroutines.*
import me.jessyan.autosize.utils.AutoSizeUtils
import java.util.regex.Pattern

/**
 *    author : yzs
 *    date   : 2022/1/1315:07
 *    desc   : kotlin 扩展函数s
 */

fun Context.toast(message: String, length: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this, message, length).show()
}

fun <T> MutableLiveData<T>.asLiveData(): LiveData<T> {
    return this
}

fun inflate(
    context: Context,
    viewId: Int,
    parent: ViewGroup? = null,
    attachToRoot: Boolean = false
): View {
    return LayoutInflater.from(context).inflate(viewId, parent, attachToRoot)
}


/**
 * author : yzs
 * time   : 2021/07/21
 * desc   : 扩展 携程
 */
fun <T> CoroutineScope.launchScope(
    block: suspend () -> T,
    success: suspend (T) -> Unit,
    failure: (suspend (Throwable) -> Unit)? = null
): Job {
    return this.launch {
        runCatching {
            withContext(Dispatchers.IO) {
                block()
            }
        }.onSuccess {
            runCatching {
                success(it)
            }.onFailure {
                failure?.invoke(it)
            }
        }.onFailure {
            failure?.invoke(it)
        }
    }
}

fun isNumeric(str: String): Boolean {
    val pattern = Pattern.compile("^(|\\+)?\\d+(\\.\\d+)?$")
    val isNum = pattern.matcher(str)
    return isNum.matches()
}


// region 缓存 size 转换的值 复用
private val sizeMap = mutableMapOf<Any, Int?>()

val Int.dp2px: Int
    get() {
        val value = sizeMap[this]
        if (value != null) return value
        val dp2px = AutoSizeUtils.dp2px(AppManager.mContext, this.toFloat())
        sizeMap[this] = dp2px
        return dp2px
    }

val Long.dp2px: Int
    get() {
        val value = sizeMap[this]
        if (value != null) return value
        val dp2px = AutoSizeUtils.dp2px(AppManager.mContext, this.toFloat())
        sizeMap[this] = dp2px
        return dp2px
    }

val Float.dp2px: Int
    get() {
        val value = sizeMap[this]
        if (value != null) return value
        val dp2px = AutoSizeUtils.dp2px(AppManager.mContext, this)
        sizeMap[this] = dp2px
        return dp2px
    }

val Double.dp2px: Int
    get() {
        val value = sizeMap[this]
        if (value != null) return value
        val dp2px = AutoSizeUtils.dp2px(AppManager.mContext, this.toFloat())
        sizeMap[this] = dp2px
        return dp2px
    }

// endregion

// region 缓存 color 转换的值 复用
private val colorMap = mutableMapOf<Int, Int?>()

val Int.toColor: Int
    get() {
        val value = colorMap[this]
        if (value != null) return value
        val color = ColorUtils.getColor(this)
        colorMap[this] = color
        return color
    }
// endregion
